package com.heima.stroke.configuration;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.RabbitListenerConfigUtils;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

//TODO:任务4.1-Rabbitmq配置
@Configuration
public class RabbitConfig {
    /**
     * 延迟时间 单位毫秒
     */
    private static final long DELAY_TIME = 1000 * 30;


    //行程超时队列
    public static final String STROKE_OVER_QUEUE = "STROKE_OVER_QUEUE";
    //行程死信队列
    public static final String STROKE_DEAD_QUEUE = "STROKE_DEAD_QUEUE";

    //行程超时队列交换器
    public static final String STROKE_OVER_QUEUE_EXCHANGE = "STROKE_OVER_QUEUE_EXCHANGE";

    //行程死信队列交换器
    public static final String STROKE_DEAD_QUEUE_EXCHANGE = "STROKE_DEAD_QUEUE_EXCHANGE";
    //行程超时交换器 ROUTINGKEY
    public static final String STROKE_OVER_KEY = "STROKE_OVER_KEY";

    //行程死信交换器 ROUTINGKEY
    public static final String STROKE_DEAD_KEY = "STROKE_DEAD_KEY";


    /**
     * 声明行程超时队列
     *
     * @return
     */
    @Bean
    public Queue strokeOverQueue() {

        //【重要配置】超时队列配置，死信队列的绑定在该方法中实现
        //需要用到以下属性：
        Map<String, Object> msg1 = new HashMap<>();
        //dead-queue参数是设置该队列的死信交换器（DLX）
        msg1.put("x-dead-letter-exchange",STROKE_DEAD_KEY);
        // x-dead-letter-routing-key  这里声明当前队列的死信路由key
        msg1.put(" x-dead-letter-routing-key",STROKE_DEAD_KEY);
        // x-message-ttl  声明队列的TTL
        msg1.put("x-message-ttl", DELAY_TIME);

        // durable:是否持久化,默认是false,持久化队列：会被存储在磁盘上，当消息代理重启时仍然存在，暂存队列：当前连接有效
        // exclusive:默认也是false，只能被当前创建的连接使用，而且当连接关闭后队列即被删除。此参考优先级高于durable
        // autoDelete:是否自动删除，当没有生产者或者消费者使用此队列，该队列会自动删除。
        return  new Queue(STROKE_OVER_QUEUE, true, true, false, msg1);
    }


    /**
     * 声明行程死信队列
     *
     * @return
     */
    @Bean
    public Queue strokeDeadQueue() {

        return new Queue(STROKE_DEAD_QUEUE);
    }

    /**
     * 创建行程超时队列交换器
     *
     * @return
     */
    @Bean
    DirectExchange strokeOverQueueExchange() {
        //定义strokeOverQueueExchange Bean，返回STROKE_OVER_QUEUE_EXCHANGE实例
        return new DirectExchange(STROKE_OVER_QUEUE_EXCHANGE);
    }

    /**
     * 创建行程死信队列交换器
     *
     * @return
     */
    @Bean
    DirectExchange strokeDeadQueueExchange() {

        return new DirectExchange(STROKE_DEAD_QUEUE_EXCHANGE);
    }



    /**
     * 行程超时队列绑定
     *
     * @return
     */
    @Bean
    Binding bindingStrokeOverDirect() {
        //定义超时队列绑定Bean，将超时Queue绑定到strokeOverQueueExchange，并设置路由键为STROKE_OVER_KEY
        return BindingBuilder
                .bind(strokeOverQueue())
                .to(strokeOverQueueExchange())
                .with(STROKE_OVER_KEY);
    }

    /**
     * 行程死信队列绑定
     *
     * @return
     */
    @Bean
    Binding bindingStrokeDeadDirect() {

        return BindingBuilder
                .bind(strokeDeadQueue())
                .to(strokeDeadQueueExchange())
                .with(STROKE_DEAD_KEY);
    }



}
